﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using SuperX.Helper;

namespace SuperX.DataService
{
    public class FileConfig
    {
        /// <summary>
        /// 日志位置
        /// </summary>
        public string LogLocation
        {
            get { return _logLocation; }
            set
            {
                if (value != _logLocation)
                {
                    _logLocation = value;
                }
            }
        }

        private string _logLocation;

        /// <summary>
        ///
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set
            {
                if (value != _userName)
                {
                    _userName = value;
                }
            }
        }

        private string _userName;

        /// <summary>
        ///
        /// </summary>
        public string Password
        {
            get { return _password; }
            set
            {
                if (value != _password)
                {
                    _password = value;
                }
            }
        }

        private string _password;

        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            if (NeedPassword)
            {
                return $"{PathType},{LogLocation},{UserName},{Password}";
            }
            return $"{PathType},{LogLocation}";
        }

        /// <summary>
        ///
        /// </summary>
        public PathType PathType
        {
            get { return _PathType; }
            set
            {
                if (value != _PathType)
                {
                    _PathType = value;
                }
            }
        }

        public bool IsEnabled { get; set; } = true;

        private PathType _PathType;

        [JsonIgnore]
        public bool NeedPassword { get => PathType != PathType.Local; }

        [JsonIgnore]
        public List<PathType> PathTypeCollection
        {
            get; set;
        } = new List<PathType> {
            PathType.Local,
            PathType.Share    };

        public FilePathInfo ToFilePathInfo()
        {
            var filePathInfo = new FilePathInfo();
            filePathInfo.SourceFileName = LogLocation;   //这个文件路径只是，只是为了触发帮助类去创建连接
            filePathInfo.SourcePathType = PathType;
            filePathInfo.SourceUserName = UserName;
            filePathInfo.SourcePassward = Password;
            if (filePathInfo.SourcePathType == PathType.Share)
            {
                filePathInfo.SourceIP = Regex.Match(LogLocation, @"(?<=^\\\\)\S{1,}?(?=[\\\/])").Value;
                filePathInfo.SourceFileName = Regex.Replace(LogLocation, @"^\\\\\S{1,}?[\\\/]", "");
            }
            else if (filePathInfo.SourcePathType == PathType.FTP)
            {
                filePathInfo.SourceIP = Regex.Match(LogLocation, @"^ftp\:\/\/\S{1,}?(?=\/)").Value;
                filePathInfo.SourceFileName = Regex.Replace(LogLocation, @"^ftp\:\/\/\S{1,}?(?=\/)", "");
            }
            return filePathInfo;
        }
    }

    public class FileAnalysisConfig
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; }

        /// <summary>
        /// 是否本地缓存
        /// </summary>
        public bool IsDbCache { get; set; }

        /// <summary>
        /// 是否备份
        /// </summary>
        public bool IsBackup { get; set; }

        ///// <summary>
        ///// 日志位置
        ///// </summary>
        public FileConfig DataFileConfig { get; set; } = new FileConfig();

        ///// <summary>
        ///// 历史日志存放位置
        ///// </summary>
        public FileConfig HistoryFileConfig { get; set; } = new FileConfig();

        /// <summary>
        /// 读取周期 ms
        /// </summary>
        public int ReadCycle { get; set; } = 2000;

        /// <summary>
        /// 历史文件过期天数
        /// </summary>
        public int LogExpireDay { get; set; } = 30;

        /// <summary>
        /// 扩展名
        /// </summary>
        public string Extension { get; set; }

        /// <summary>
        /// 文件在目标文件夹下的层级，当前文件夹下为第1层
        /// </summary>
        public int Floor { get; set; }
    }

    public enum WorkModel
    {
        /// <summary>
        /// 追加模式
        /// </summary>
        [Description("追加模式")]
        Append,

        /// <summary>
        /// 新建模式
        /// </summary>
        [Description("新建模式")]
        CreateNew,

        /// <summary>
        /// 覆盖
        /// </summary>
        [Description("覆盖")]
        Cover
    }
}